package ratson.utils;

import java.util.Arrays;

public class CyclicArrayList<ItemType> {
	ItemType[] data;
	int headPos = 0;
	int tailPos = 0;
	int actualSize = 0;
	
	public CyclicArrayList(int size){
		if (size <=0 )
			throw new ArrayIndexOutOfBoundsException("Array size must be >0");
		data = (ItemType[]) new Object[size];
	}
	public void add(ItemType i){
		data[headPos] = i;
		headPos++;
		if (headPos > data.length){
			headPos = 0;
		}
		if (isFull()){
			tailPos ++;
			if (tailPos > data.length)
				tailPos = 0;
		}
	}
	
	public ItemType get(int idx){
		int i1 = headPos - idx;
		if (i1 < 0)
			i1 += data.length;
		return data[i1];
	}
	public ItemType getHead(){
		return data[headPos];
	}
	public ItemType getTail(){
		return data[tailPos];
	}
	public int size(){
		return actualSize;
	}
	public int capacity(){
		return data.length;
	}
	public boolean isEmpty(){
		return actualSize == 0;
	}
	public void clear(){
		Arrays.fill(data, null);
		headPos = 0;
		tailPos = 0;
	}
	public boolean isFull() {
		return actualSize == data.length;
	}
}
